using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    public static UIManager Instance;
    public Text money_text;//分数文本
    public Text money_tips;//分数提示文本
    public float money_sum = 0;//分数
    float time;

    float level = 1;//关卡
    float level_time;
    public Text level_time_text;
    public Text level_text;

    public float target_money = 500;//目标金额
    public Text target_money_text;

    //下一关界面
    public GameObject next;
    public GameObject restart;

    private void Awake() {
        Instance = this;
        level_time = 30;
        money_text.text = "$: " + money_sum.ToString();
        level_text.text = "Level: " + level.ToString();
        target_money_text.text = "To: " + target_money.ToString();
    }

    private void Update()
    {
        if (time > 0)
        {
            time -= Time.deltaTime;
            money_tips.gameObject.SetActive(true);
        }
        else
        {
            money_tips.gameObject.SetActive(false);
        }

        if(level_time > 0){
            level_time -= Time.deltaTime;
            level_time_text.text = "T: " + ((int)level_time).ToString();
        }else{
            level_time = 0;
            Time.timeScale = 0;//暂停游戏
            
            //判断是否达标
            if(money_sum >= target_money){
                next.SetActive(true);
            }else{
                restart.SetActive(true);
            }
        }
    }

    public void SetScore(float money)
    {
        time = 2;
        money_tips.text = "+ " + money.ToString();
        money_sum += money;
        money_text.text = "$: " + money_sum.ToString();
    }

    //下一关
    public void NextLevel(){
        ItemSpawner.Instance.SpawnItems();

        level_time = 30;
        level++;
        level_text.text = "Level: " + level.ToString();

        target_money += 1000;
        target_money_text.text = "To: " + target_money.ToString();

        Time.timeScale = 1;
        next.SetActive(false);

        Hook.Instance.Reset();
    }

    //重新开始
    public void RestartLevel(){
        SceneManager.LoadScene("Menu");
    }

}
